﻿@using Fluxor.Blazor.Web.Components;
@using SimpleIdServer.IdServer.Api.RegistrationWorkflows;
@using SimpleIdServer.IdServer.Website.Resources;
@using SimpleIdServer.IdServer.Website.Stores.RegistrationWorkflowStore;
@inherits FluxorComponent
@inject DialogService dialogService
@inject NotificationService notificationService
@inject IDispatcher dispatcher;

<EditRegistrationWorkflow Saved="@Save" IsEditionMode=false IsUpdating=@isUpdating></EditRegistrationWorkflow>

@code {
    bool isUpdating { get; set; }  = false;
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        SubscribeToAction<AddRegistrationWorkflowSuccessAction>(async (act) =>
        {
            isUpdating = false;
            dialogService.Close();
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = string.Format(Global.RegistrationWorkflowAdded, act.Name) });
            StateHasChanged();
        });
        SubscribeToAction<AddRegistrationWorkflowFailureAction>(async (act) =>
        {
            isUpdating = false;
            notificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = act.ErrorMessage });
            StateHasChanged();
        });
    }

    void Save(RegistrationWorkflowResult registrationWorkflow)
    {
        isUpdating = true;
        var act = new AddRegistrationWorkflowAction { IsDefault = registrationWorkflow.IsDefault, Name = registrationWorkflow.Name, Steps = registrationWorkflow.Steps };
        dispatcher.Dispatch(act);
    }
}
